package com.funmi.lizip.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.funmi.lizip.common.base.R;
import com.funmi.lizip.common.base.ResultCode;
import com.funmi.lizip.common.exception.ApplicationException;
import com.funmi.lizip.entity.Options;
import com.funmi.lizip.entity.Task;
import com.funmi.lizip.entity.TaskItem;
import com.funmi.lizip.entity.dto.task.TaskQueryRequest;
import com.funmi.lizip.entity.dto.taskItem.TaskItemQueryRequest;
import com.funmi.lizip.entity.vo.TaskItemVo;
import com.funmi.lizip.entity.vo.TaskVo;
import com.funmi.lizip.mapper.OptionsMapper;
import com.funmi.lizip.mapper.TaskItemMapper;
import com.funmi.lizip.mapper.TaskMapper;

import com.funmi.lizip.service.TaskItemService;
import com.funmi.lizip.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
@Transactional
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {


    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private TaskItemMapper itemMapper;

    @Autowired
    private OptionsMapper optionsMapper;


    @Override
    public TaskVo listById(TaskQueryRequest request) {
        // 返回taskVo
        TaskVo taskVo = new TaskVo();

        // 根据request.getId查询task
        Task task = this.getById(request.getId());
      //  Task task = taskMapper.selectById(request.getId());
        if (task == null) {
            throw new ApplicationException(ResultCode.ID_NOT_FOUND);
        }
        // 给taskItem赋task的id
        TaskItem taskItem = new TaskItem();
        taskItem.setTaskId(task.getId());
        // 根据taskItem查询所有
        QueryWrapper<TaskItem> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("task_id", request.getId());
        List<TaskItem> taskItems = itemMapper.selectList(queryWrapper);
        // 创建一个TaskItemVoS封装
        ArrayList<TaskItemVo> taskItemVos = new ArrayList<>();

        for (TaskItem item : taskItems) {
            // 给options封装
            Options options = new Options();
            options.setTaskItemId(item.getId());
            QueryWrapper<Options> qw = new QueryWrapper<>();
            qw.eq("task_item_id", item.getId());
            List<Options> optionsList = optionsMapper.selectList(qw);
            // 封装
            TaskItemVo taskItemVo = new TaskItemVo();
            taskItemVo.setId(item.getId());
            taskItemVo.setTaskId(item.getTaskId());
            taskItemVo.setTaskItemName(item.getTaskItemName());
            taskItemVo.setType(item.getType());
            taskItemVo.setIsRequired(item.getIsRequired());
            taskItemVo.setSort(item.getSort());
            taskItemVo.setCreateAt(item.getCreateAt());
            taskItemVo.setIsDelete(item.getIsDelete());
            taskItemVo.setOptions(optionsList);
            // 装进taskItemVos里面
            taskItemVos.add(taskItemVo);
        }

        // 封装taskVo
        taskVo.setTaskName(task.getTaskName());
        taskVo.setId(task.getId());
        taskVo.setTaskStatus(task.getTaskStatus());
        taskVo.setIsDelete(task.getIsDelete());
        taskVo.setTaskDescription(task.getTaskDescription());
        taskVo.setCreateAt(task.getCreateAt());
        taskVo.setMapOptions(task.getMapOptions());
        taskVo.setItemList(taskItemVos);

        return taskVo;

    }
}
